import request from '@/utils/request';
import type {
  LoginForm,
  RegisterForm,
  PasswordUpdateForm,
  UserUpdateForm,
  User,
  ApiResponse
} from '@/types/user';

// 发送注册验证码
export const sendRegisterCode = (email: string): Promise<ApiResponse> => {
  return request.post('/users/register/code', { email });
};

// 用户注册
export const register = (data: RegisterForm): Promise<ApiResponse<User & { token: string }>> => {
  return request.post('/users/register', data);
};

// 用户登录
export const login = (data: LoginForm): Promise<ApiResponse<User & { token: string }>> => {
  return request.post('/users/login', data);
};

// 修改密码
export const updatePassword = (data: PasswordUpdateForm): Promise<ApiResponse> => {
  return request.put('/users/password', data);
};

// 上传头像
export const uploadAvatar = (file: File): Promise<ApiResponse<string>> => {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/users/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

// 获取当前用户信息
export const getCurrentUser = (): Promise<ApiResponse<User>> => {
  return request.get('/users/current');
};

// 更新用户信息
export const updateUserInfo = (data: UserUpdateForm): Promise<ApiResponse<User>> => {
  return request.put('/users', data);
};
